//
//  policyConsultView.m
//  Project two framework
//
//  Created by rimi on 16/7/23.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "policyConsultView.h"
#import "policyConsultCell.h"
#import "ZPZNetRequest.h"
#import "LStringUtils.h"
#import "AFNetworking.h"
#import "PolicyConsultContentView.h"
#import "ZPZNetRequest+MealsList.h"
#import "MJRefresh.h"
#import "DynamicView.h"
@interface policyConsultView ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

/**头部阴影*/
@property(nonatomic,strong)UIImageView *headImage;
/**尾部阴影*/
@property(nonatomic,strong)UIImageView *foodImage;

@property(nonatomic,strong)UILabel *label1;
@property(nonatomic,strong)UILabel *label2;
@property(nonatomic,strong)PolicyConsultContentView *policyConsultContentView;
@property(nonatomic,strong)DynamicView *dyNameIcView;

@property(nonatomic,strong)NSMutableArray *textFieldArray;

@end

@implementation policyConsultView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self ) {
        self.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(45, 120, 825, 455);
//        _array = [[NSMutableArray alloc]init];
        [self tableView];
        [self headImage];
        [self foodImage];
        [self label2];
        [self label1];
        _policyConsultContentView = [[PolicyConsultContentView alloc]init];
        _policyConsultContentView.hidden = YES;
        [self addSubview:_policyConsultContentView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshDataSource:) name:@"12" object:nil];
//        _textFieldArray = []
    }

    return self;
}


#pragma mark -- 实现通知
- (void)refreshDataSource:(NSNotification *)info
{
//    [_array_Policy removeAllObjects];
    [ZPZNetRequest getAdviceListPageSize:@"10" PageNum:@"1" byTitle:@"" byKeywords:info.userInfo[@"textField"] comletion:^(id object, BOOL success) {
        if (success) {
            [_array_Policy removeAllObjects];
                        NSLog(@"%@",object);
            _array_Policy = [NSMutableArray arrayWithArray:object[@"content"]];
//            _textFieldArray = [NSMutableArray arrayWithArray:object[@"content"][@"keywords"]];
            
            [_tableView reloadData];
        }else{
        
//            NSLog(@"123123123");
        }
        
        
    }];
}


#pragma mark --cell点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < _array_Policy.count) {
        self.dyNameIcView = [DynamicView showOtherView];
        _dyNameIcView.nameLabel.text = _array_Policy[indexPath.row][@"title"];
       
        _dyNameIcView.content.text = _array_Policy[indexPath.row][@"content"];
        CGRect rect = [_dyNameIcView.content.text boundingRectWithSize:CGSizeMake(630, 500) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
        _dyNameIcView.content.frame = CGRectMake(50, 100, 650, rect.size.height);

    }else{
    
        return;
    }
    

    
    

}



#pragma mark --数据源
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_array_Policy.count < 8) {
        return 8;
    } else {
        return _array_Policy.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndex = @"cellllllllll";
    policyConsultCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndex];
    if (cell == nil) {
        cell = [[policyConsultCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndex];
        
    }

    if (indexPath.row < _array_Policy.count) {
        cell.titleLabel.text = _array_Policy[indexPath.row][@"title"];
        cell.serialNumberLabel.text = [NSString stringWithFormat:@"%ld",(indexPath.row+1)];
    }else{
        cell.titleLabel.text = @"";
        cell.serialNumberLabel.text = @"";
    }
 

    return cell;
}



-(void)initializeDataSource{
    [ZPZNetRequest getAdviceListPageSize:@"10" PageNum:@"1" byTitle:@"" byKeywords:@"" comletion:^(id object, BOOL success) {
        if (success) {
//            NSLog(@"%@",object);
            _array_Policy= [NSMutableArray arrayWithArray:object[@"content"]];
            
            [_tableView reloadData];
        }

        
    }];
    
}

-(void)refreshHeader{
    [ZPZNetRequest getAdviceListPageSize:@"10" PageNum:@"1" byTitle:@"" byKeywords:@"" comletion:^(id object, BOOL success) {
        if (success) {
            //            NSLog(@"%@",object);
            _array_Policy= [NSMutableArray arrayWithArray:object[@"content"]];
             [_tableView.mj_header endRefreshing];
            [_tableView reloadData];
        }
        
        
    }];



}


-(UILabel *)label1
{
    if (_label1 == nil) {
        _label1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, 60, 40)];
        _label1.text = @"序号";
        _label1.font = [UIFont boldSystemFontOfSize:16];
        _label1.textColor = [UIColor whiteColor];
        [self addSubview:_label1];
    }
    return _label1;
}

-(UILabel *)label2
{
    if (_label2 == nil) {
        _label2 = [[UILabel alloc]initWithFrame:CGRectMake(470, 0, 60, 40)];
        _label2.text = @"标题";
        _label2.font = [UIFont boldSystemFontOfSize:16];
        _label2.textColor = [UIColor whiteColor];
        [self addSubview:_label2];
    }
    return _label2;
}

-(UIImageView *)headImage{
    if (_headImage == nil) {
        _headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 40, 845, 10)];
        _headImage.image = [UIImage imageNamed:@"up_19.png"];
        [self addSubview:_headImage];
    }
    return _headImage;
}

-(UIImageView *)foodImage{
    if (_foodImage == nil) {
        _foodImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 435, 845, 10)];
        _foodImage.image = [UIImage imageNamed: @"down_27.png"];
        [self addSubview:_foodImage];
    }
    return _foodImage;
}


-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 40, 825, 400) style:UITableViewStylePlain];
        _tableView.rowHeight = 50;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self initializeDataSource];
        [self addSubview:_tableView];
        //上拉刷新的方法
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self refreshHeader];
        }];

    }

    return _tableView;
}
@end
